function transform(list) {
  if (!(list instanceof Array)) {
    return [];
  }
  return list.map((item) => {
    const { id, name } = item;
    return { value: id, label: name };
  });
}

(function () {
  let num = 0;
  let req;
  let lastTime = performance.now();

  function addCount() {
    const now = performance.now();
    const duration = now - lastTime;
    if (duration >= 1000) {
      lastTime = lastTime + 1000;
      num++;
      console.log(num);
      if (num >= 10) {
        window.cancelAnimationFrame(req);
        return;
      }
    }
    req = window.requestAnimationFrame(addCount);
  }

  req = window.requestAnimationFrame(addCount);
})();

(function () {
  let num = 0;
  let timer = setInterval(() => {
    num++;
    console.log(num, performance.now());
    if (num >= 10) {
      clearInterval(timer);
      timer = null;
    }
  }, 1000);
})();

const PENDING = "pending";
const REJECTED = "reject";
const FULLFILLED = "fullfilled";
function resolvePromise(promise, x, resolve, reject) {
  if (promise === x) {
    throw new Error("then返回的promise不能是自己，要卡死");
  }

  if (x instanceof MyPromise) {
    if (x.state === FULLFILLED) {
      resolve(x.value);
    } else if (x.state === REJECTED) {
      reject(x.reason);
    } else if (x.state === PENDING) {
      x.then((y) => {
        resolvePromise(promise, y, resolve, reject);
      });
    }
  } else if (x !== null && (typeof x === "object" || typeof x === "function")) {
    let excuted;
    try {
      const then = x.then;
      // thenable处理
      if (typeof then === "function") {
        then.call(
          x,
          (y) => {
            if (excuted) return;
            excuted = true;
            resolvePromise(promise, y, resolve, reject);
          },
          (e) => {
            if (excuted) return;
            excuted = true;
            reject(e);
          }
        );
      }
    } catch (reason) {
      if (excuted) return;
      excuted = true;
      reject(reason);
    }
  } else {
    resolve(x);
  }
}
class MyPromise {
  constructor(excute) {
    this.state = PENDING;
    this.value = "";
    this.reason = "";
    this.onFulfilledFns = [];
    this.onRejectedFns = [];

    const resolve = (value) => {
      if (this.state === PENDING) {
        setTimeout(() => {
          this.state = FULLFILLED;
          this.value = value;
          this.onFulfilledFns.forEach((fn) => fn(value));
        });
      }
    };

    const reject = (reason) => {
      if (this.state === PENDING) {
        setTimeout(() => {
          this.state = REJECTED;
          this.reason = reason;
          this.onRejectedFns.forEach((fn) => fn(reason));
        });
      }
    };

    try {
      excute(resolve, reject);
    } catch (err) {
      reject(err);
    }
  }
  then(onFulfilled, onRejected) {
    let promise;
    let x;

    if (this.state === FULLFILLED) {
      promise = new MyPromise((resolve, reject) => {
        try {
          x = onFulfilled(this.value);
          resolvePromise(promise, x, resolve, reject);
        } catch (err) {
          reject(err);
        }
      });
    } else if (this.state === REJECTED) {
      try {
        x = onReject(this.reason);
        resolvePromise(promsie, x, resolve, reject);
      } catch (reason) {
        reject(reason);
      }
    } else if (this.state === PENDING) {
      promise = new MyPromise((resolve, reject) => {
        this.onFulfilledFns.push(() => {
          try {
            x = onFulfilled(this.value);
            resolvePromise(promise, x, resolve, reject);
          } catch (err) {
            reject(err);
          }
        });
        this.onRejectedFns.push(() => {
          try {
            x = onReject(this.reason);
            resolvePromise(promsie, x, resolve, reject);
          } catch (reason) {
            reject(reason);
          }
        });
      });
    }

    return promise;
  }
  catch(onError) {
    this.then(null, (reason) => {
      onError(reason);
    });
  }
  finally(onFinally) {
    this.then(
      (value) => {
        MyPromise.resolve(onFinally()).then(() => value);
      },
      (reason) => {
        MyPromise.reject(onFinally()).then(() => {
          throw reason;
        });
      }
    );
  }
}

MyPromise.resolve = (value) => {
  if (value instanceof MyPromise) {
    return value;
  }
  return new MyPromise((resolve) => {
    return resolve(value);
  });
};

MyPromise.reject = (reason) => {
  return new MyPromise((resolve, reject) => {
    reject(reason);
  });
};
